#!/usr/bin/env python
import roslib; roslib.load_manifest('ua_objdb')

from ua_objdb.srv import *
import rospy

import os
import cPickle as pickle



from objdb import ObjDB



# file where the object db is stored
filename = 'objdb.pkl'
db = None

def handle_fetch_object(req):
    global db
    return FetchObjectResponse(db[req.name])

def handle_save_object(req):
    global db
    db[req.name] = req.obj
    #db.save()
    return SaveObjectResponse(True)

def handle_get_names(req):
    global db
    return GetNamesResponse(list(db))

def objdb_server():
    global db
    db = ObjDB(filename)
    #db.load()
    rospy.init_node('objdb_server')
    rospy.Service('fetch_object', FetchObject, handle_fetch_object)
    rospy.Service('save_object', SaveObject, handle_save_object)
    rospy.Service('get_names', GetNames, handle_get_names)
    print 'Object database is ready.'
    rospy.spin()

if __name__ == '__main__':
    objdb_server()

